package cn.calm.parameter.action;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.struts2.osgi.interceptor.ServiceAware;

import cn.calm.commons.action.CurdAction;
import cn.calm.commons.entity.PageMode;
import cn.calm.entity.Parameter;
import cn.calm.parameter.service.IParameterService;

@SuppressWarnings("rawtypes")
public class ParameterAction<T extends Parameter, S extends IParameterService<T>>
extends CurdAction<T, S> implements ServiceAware<IParameterService> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String add() {
		String id = getT().getId().toUpperCase();
		getT().setId(id);
		String result = super.add();
		if (getT().isPutSession() == 1) {
			setSession(getT().getId(), getT().getRealValue());
		}
		return result;
	}

	@Override
	public String update() {
		String result = super.update();
		removeSession(getT().getId());
		if (getT().isPutSession() == 1) {
			setSession(getT().getId(), getT().getRealValue());
		}
		return result;
	}

	@Override
	public String delete() {
		removeSession(getT().getId());
		return super.delete();
	}

	@Override
	public List<T> getList() {
		return super.getList();
	}

	@Override
	public Map<String, List<String>> getFieldErrors() {
		return super.getFieldErrors();
	}

	@Override
	public Collection<String> getActionErrors() {
		return super.getActionErrors();
	}

	@Override
	public T getT() {
		return super.getT();
	}

	@Override
	public PageMode getPageMode() {
		return super.getPageMode();
	}

	@Override
	public String getModeName() {
		return "parameter";
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setServices(List<IParameterService> services) {
		if (service == null) {
			if (services != null && services.size() > 0) {
				service = (S) services.get(0);
			}
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class<T> getEntityClass() {
		return (Class<T>) Parameter.class;
	}
}
